package com.isotronica.neveras.logica;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class VirtualConsoleManager extends Thread{
	
	private Socket socket;
	private Controlador miControlador;
	private InputStream inputStream;
	private OutputStream outputStream;
	private Thread t;
	private int conTimeOut;
	private BufferedReader in;
	private PrintWriter out;
	
	
	public VirtualConsoleManager(Socket socket, Controlador miControlador) {
		
		this.socket = socket;
		this.miControlador = miControlador;
		
		try {
			 in = new BufferedReader(new InputStreamReader(
					 this.socket.getInputStream()));
			 	
			outputStream = this.socket.getOutputStream();
			outputStream = this.socket.getOutputStream();
			out = new PrintWriter(outputStream, 
	                 true);

		} catch (IOException e) {
			e.printStackTrace();
		}
		t = new Thread(this);
		t.setPriority(NORM_PRIORITY);
		t.start();
	}

	

	public void run() {
		
		
	
		while (conTimeOut < 120) {
			try {
				sleep(5000);
			} catch (InterruptedException e) {
			}
			conTimeOut++;
			try {
				String mensaje = "";
				while (in.ready()) {
					mensaje = mensaje +"\n\r" + in.readLine();
					out.println("ok");
					conTimeOut = 0;					
				}
				if (conTimeOut == 0)
				{
					miControlador.setVirtualConsole(mensaje);
					System.out.println("Datos Ok");
				}
					
				
			}catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println("Fin Datos");
	}
		

}
